﻿
Partial Class WebForm_ManageVolunteer_SearchChangeRecord
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLChangeType()
        End If
        gv.Attributes.Add("style", "word-break:break-all;word-wrap:break-word")
    End Sub
    Protected Sub BindDDLChangeType()
        Me.ddlChangeType.DataTextField = "Text"
        Me.ddlChangeType.DataValueField = "Value"
        Me.ddlChangeType.DataSource = MyObj.GetCodeMappingData("Volunteer", "ChangeRecord")
        Me.ddlChangeType.DataBind()

        Me.ddlChangeType.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub


    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub
    Public Sub BindGV()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("select  distinct cr.ChangeID,v.VolunteerName,cm.Text as ChangeTypeText,cr.ChangeReason ")
        sql.AppendFormat(",cr.CreateDate,av.OrganizationName ")
        sql.AppendFormat(",case when cr.CreateBy= 'System' then cr.CreateBy else av.UserName end as UserName  ")
        sql.AppendFormat("from ChangeRecord cr ")
        sql.AppendFormat("inner join Volunteer v on cr.ChangeID=v.VolunteerID ")
        sql.AppendFormat("left join VOrganizationViewV2 vo on cr.VOrganizationID=vo.VOrganizationID ")
        sql.AppendFormat("inner join Codemapping cm on cr.ChangeType =cm.Value ")
        sql.AppendFormat("and TableName='Volunteer' and ColumnName='ChangeRecord' ")
        sql.AppendFormat("left join AdminVorganizationView av on cr.CreateBy = av.UserID ")
        sql.AppendFormat("where 1=1 ")

        If Me.VolunteerQueryControl1.OrganizationID <> "0" Then
            sql.AppendFormat("and vo.DirectUOrganizationID='{0}' ", Me.VolunteerQueryControl1.OrganizationID)
        End If

        If Me.VolunteerQueryControl1.VOrganizationID <> 0 Then
            sql.AppendFormat("and cr.VOrganizationID={0} ", Me.VolunteerQueryControl1.VOrganizationID)
        End If

        If Me.VolunteerQueryControl1.VolunteerId <> "" Then
            sql.AppendFormat("and cr.ChangeID = '{0}' ", Me.VolunteerQueryControl1.VolunteerId)
        End If
     
        If Me.VolunteerQueryControl1.VolunteerName <> "" Then
            sql.AppendFormat("AND ltrim(replace(replace(v.VolunteerName, '  ', ''), ' ', '')) like '%{0}%'  ", LTrim(Me.VolunteerQueryControl1.VolunteerName.Replace("  ", "").Replace(" ", "")))
        End If

        If ddlChangeType.SelectedValue <> "" Then
            sql.AppendFormat("AND cr.ChangeType ='{0}' ", ddlChangeType.SelectedValue)
        End If

        ' If Me.txtCreateDateStart.Value.ToString.Trim() <> "01.01.01" AndAlso Me.txtCreateDateEnd.Value.ToString.Trim() <> "01.01.01" Then
        'Dim CreateDateStart As String = Int32.Parse(Me.txtCreateDateStart.Value.ToString.Split(".")(0)) + 1911 & Right(Me.txtCreateDateStart.Value.ToString.Trim(), 6)
        'Dim CreateDateEnd As String = Int32.Parse(Me.txtCreateDateEnd.Value.AddDays(1).ToString.Split(".")(0)) + 1911 & Right(Me.txtCreateDateEnd.Value.AddDays(1).ToString.Trim(), 6)
        sql.AppendFormat("AND cr.CreateDate between '{0:yyyy/MM/dd}' and '{1:yyyy/MM/dd}' ", Me.txtCreateDateStart.Value, Me.txtCreateDateEnd.Value.AddDays(1))
        ' End If

        sql.AppendFormat(gv.OrderBySQL)
        gv.DataSource = dat.GetData(sql)
        gv.DataBind()
    End Sub


End Class
